package com.cn.action;

import com.cn.entity.Card;
import com.cn.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.service.UserService;
import com.cn.entity.User;

import java.util.HashMap;
import java.util.Map;

@Controller
public class UserAction {
	@Autowired
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	@Autowired
	private CardService cardService;

	public CardService getCardService() {
		return cardService;
	}

	public void setCardService(CardService cardService) {
		this.cardService = cardService;
	}

	// 用户登录
	// 获取:userName,userPwd
	// 返回:失败:User(-1)
	// 成功:User详细信息
	@RequestMapping("/userLoginAction")
	@ResponseBody
	public User userLoginAction(User user) {
		System.out.println("登录用户，用户名：" + user.getUserAccount());

		User u = userService.selectUser(user);

		if (u == null) {
			return new User(-1);
		}
		return u;
	}

	// 用户注册
	// 获取:userAccount,userPwd
	// 返回:0-用户名已存在 1-注册成功
	@RequestMapping("/userRegisterAction")
	@ResponseBody
	public int userRegisterAction(User user) {
		System.out.println("注册用户" + user);
		if (userService.selectUserAccount(user.getUserAccount()) == null) {
			userService.insertUser(user);
			return 1;
		} else {
			return 0;// 用户名重复,失败
		}

	}

	// 获取用户信息
	// 获取：userId
	// 返回：userName,userAccount,userPwd,userScore,userMoney.userCreate
	@RequestMapping("/findUserInforAction")
	@ResponseBody
	public User findUserInforAction(int userId) {
		System.out.println("用户信息" + userId);
		return userService.getUserInfor(userId);

	}

	// 更新用户密码
	// 获取：userId,userPwd
	// 返回：无
	@RequestMapping("/alterUserPwdAction")
	@ResponseBody
	public void alterUserPwdAction(User user) {
		System.out.println("更新用户密码" + user);
		userService.updateUserPwd(user);
	}

	// 更新用户信息
	// 获取：userId,userName
	// 返回：无
	@RequestMapping("/alterUserInforAction")
	@ResponseBody
	public void alterUserInforAction(User user) {
		System.out.println("更新用户信息" + user);
		userService.updateUserInfor(user);
	}
	// 绑定银行卡
	@RequestMapping("/bindCard")
	@ResponseBody
	public int bindCard(Card card) {
		System.out.println("绑定银行卡" + card.getCardUseracc());
		Map<String, Object> map = new HashMap<String, Object>();

		int key;

		//搜不到银行卡
		if (cardService.getCardByNum(card.getCardNum()).equals(null)) {
			key = 0;
		}
		//银行卡已经被绑定
		else if (!cardService.getAccountByNum(card.getCardNum()).equals("0")) {
			key = 1;
		}
		//密码或者姓名错误
		else if (cardService.findCard(card).equals(null)) {
			key = 2;
		}
		//绑定成功
		else {
			cardService.setCard(card);
			key = 3;
		}

		return key;
	}
		// 解绑
		@RequestMapping("/unboundCard")
		@ResponseBody
		public void unboundCard(Card card) {
			System.out.println("解绑银行卡" + card.getCardUseracc());
			cardService.unboundCard(card);
	}

}
